package models

import (
	"gorm.io/gorm"
	"learn-srv/basic/config"
)

type Order struct {
	gorm.Model
	UserId     int     `json:"user_id" gorm:"type:int;comment:'用户id'"`
	CourseId   int     `json:"course_id" gorm:"type:int;comment:'课程id'"`
	OrderSn    string  `json:"order_sn" gorm:"type:varchar(50);comment:'订单编号'"`
	OrderPrice float64 `json:"order_price" gorm:"type:decimal(10,2);comment:'订单价格'"`
	Number     int     `json:"number" gorm:"type:int;comment:'购买数量'"`
	Status     int     `json:"status" gorm:"type:int;comment:'订单状态 1-待支付 2-交易超时 3-交易支付成功 4-交易结束'"`
}

func (o *Order) TableName() string {
	return "order"
}

func (o *Order) CreateOrder() error {
	return config.DB.Create(&o).Error
}

func (o *Order) GetOrderById(id, userId int) error {
	return config.DB.Model(&Order{}).Where("id = ? and user_id = ?", id, userId).Limit(1).Find(&o).Error
}

func (o *Order) GetOrderInfo(orderSn string) error {
	return config.DB.Model(&Order{}).Where("order_sn = ?", orderSn).Limit(1).Find(&o).Error
}

func (o *Order) UpdateOrderStatus(orderSn string) error {
	return config.DB.Model(&Order{}).Where("order_sn = ?", orderSn).Updates(&o).Find(&o).Error
}
